home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / jaz_clib.arc / JZINTHND.ASM < prev    next >
Assembly Source File  |  1989-04-09  |  4KB  |  160 lines

  1. Comment *
  2. ┌────────────────────────────────────────────────────────────────────────────┐
  3. │jzinthnd.asm                                     │
  4. │This routine provides a generic interrupt interface for c.             │
  5. │This routine takes care of the task of saving the registers and         │
  6. │calling the user routine. Use jzinsint.c to install the interrupt handler   │
  7. │                                         │
  8. │ (C) JazSoft Software by Jack A. Zucker (301) 794-5950              │
  9. └────────────────────────────────────────────────────────────────────────────┘
  10. *
  11.  
  12. ;=============================================================================
  13. ;                  Definitions
  14. ;=============================================================================
  15.  
  16. TREG struc
  17.   wax      dw      ?
  18.   wbx      dw      ?
  19.   wcx      dw      ?
  20.   wdx      dw      ?
  21.   wsi      dw      ?
  22.   wdi      dw      ?
  23.   wds      dw      ?
  24.   wes      dw      ?
  25.   wflags  dw      ?
  26. TREG ends
  27.  
  28. ;=============================================================================
  29. ;                    Equates
  30. ;=============================================================================
  31.  
  32. STACKSIZE equ    1000h            ; 4 kb stack size
  33.  
  34. ;=============================================================================
  35. ;                    Data
  36. ;=============================================================================
  37.  
  38. DGROUP    group    _DATA
  39. _DATA    segment word public 'DATA'
  40.     assume    ds:DGROUP
  41.     extrn  STKHQQ:word        ; upper stack limit (defined by MS/C)
  42.     public ISTACK,STKINCR
  43.  
  44. STKINCR   dw       0            ; inc value for recursive interrupts
  45. ISTACK      db STACKSIZE dup ('STACK')    ; Local stack for interrupts
  46. WREG      TREG    <>            ; structure for arg to int handler
  47.  
  48. _DATA    ends
  49.  
  50. ;=============================================================================
  51. ;                   Code
  52. ;=============================================================================
  53.  
  54.     assume cs:_text
  55. _text    segment public byte 'code'
  56.  
  57.     ; the following 3 are declared public so MS/C can reach them:
  58.     public _jzinthnd,_gdseg,_gfunction
  59.  
  60.     ; the next set up public stuff is just for debugging:
  61. public    stacksize, stkincr, istack, _jzinthnd, savebp, savesp, saveds, savess
  62. public    _gdseg, gdseg, _gfunction, gfunction,wreg
  63.  
  64.  
  65. _jzinthnd proc near
  66.  
  67. mov cs:savebp,bp            ; save users registers
  68. mov cs:savesp,sp            ; in code segment variables
  69. mov cs:savess,ss
  70. mov cs:saveds,ds
  71.  
  72. push cs:gdseg                ; recover c data segment
  73. pop ds
  74.  
  75.                     ; put register values in struc
  76. pushf
  77. pop WREG.wflags
  78. mov WREG.wax,ax
  79. mov WREG.wbx,bx
  80. mov WREG.wcx,cx
  81. mov WREG.wdx,dx
  82. mov WREG.wsi,si
  83. mov WREG.wdi,di
  84. push cs:saveds
  85. pop WREG.wds
  86. mov WREG.wes,es
  87. pushf
  88. pop WREG.wflags
  89.  
  90. cli                    ; no interrupts here
  91. mov ax,DGROUP                ; get new interrupt stack
  92. mov ss,ax                ; over local stack space
  93. mov sp,offset ISTACK
  94. add STKINCR,STACKSIZE            ; Stack grows downward so we
  95. add sp,STKINCR                ; start at the end of the buffer
  96. sti                    ; interrupts back on
  97.  
  98. push STKHQQ                ; save ms-c stack limit
  99. mov ax,sp                ; get top of stack
  100. sub ax,STACKSIZE
  101. add ax,4                ; get past "push STKHQQ" and
  102.                     ; add 2 for good measure.
  103.  
  104. mov STKHQQ,ax                ; tell MS/C about it
  105.  
  106. Lea ax,wreg                ; get address of register structure
  107. push ax                 ; and put it on stack for c function
  108. mov ax,WREG.wax             ; restore orig ax
  109.  
  110. cld                    ; MS/C assumes this
  111.  
  112. mov bx,offset gfunction         ; get address of c function
  113. call [bx]                ; call it indirectly !
  114.  
  115. add sp,2                ; get address of WREG off Stack
  116.  
  117. push ax                 ; save value. Later version will use it
  118. sub STKINCR,STACKSIZE            ; decrement for exit from recursion
  119. pop ax                    ; restore return value (not used yet)
  120. pop STKHQQ                ; Give Ms/C back it's orig stack limit
  121.  
  122. mov bx,WREG.wbx             ; restore users registers
  123. mov cx,WREG.wcx             ; from code segment
  124. mov dx,WREG.wdx
  125. mov bp,cs:savebp
  126. mov si,WREG.wsi
  127. mov di,WREG.wdi
  128. mov ax,WREG.wes
  129. mov es,ax
  130. push WREG.wflags
  131. popf
  132. cli                    ; no interrupts here
  133. mov ax,cs:savess            ; restore original stack segment
  134. mov ss,ax
  135. mov sp,cs:savesp            ; and offset
  136. sti                    ; allow interrupts now
  137.  
  138. mov ax,cs:saveds            ; restore interrupt data segment
  139. mov ds,ax
  140.  
  141. mov ax,WREG.wax             ; restore ax
  142.  
  143. iret                    ; return back to caller
  144.  
  145. savebp    dw    0
  146. savesp    dw    0
  147. saveds    dw    0
  148. savess    dw    0
  149.  
  150. _gdseg       label far
  151. gdseg       dw      0               ; hold global data segment
  152.  
  153. _gfunction label far
  154. gfunction  dw      0               ; place to hold function address
  155.  
  156. _jzinthnd    endp
  157.  
  158. _text    ends
  159. end
  160.